﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraLayout;
using DiaoPaiDaYin;
using Luna小店.UC.门店下单.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Luna小店.UC.设计点
{
    public class RepositoryItemSearchLookUpEdit设计点 :RepositoryItemSearchLookUpEdit
    {
        #region repositoryItemSearchLookUpEdit1View;
        private DevExpress.XtraGrid.Views.Grid.GridView repositoryItemSearchLookUpEdit1View = new DevExpress.XtraGrid.Views.Grid.GridView();
        #endregion
        public DTODesignPoints Dto设计点;


        public String ITEM_CD { get; set; }
        public String OPTION_VALUE { get; set; }
        public String ITEM_NAME_CN { get; set; }
        public String ITEM_VALUE { get; set; }
        public RepositoryItemSearchLookUpEdit设计点(DataRow dr, DTODesignPoints Dto设计点) {
            this.Dto设计点 = Dto设计点;   //  传入当前设计点
            this.addColumns();  //  下拉view的column

            //this.Appearance.Font = new System.Drawing.Font("Tahoma", 15F);
            this.AutoHeight = false;
            this.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
            this.Name = "repositoryItemSearchLookUpEdit" + dr["ITEM_VALUE"].ToString() ;
            this.PopupView = this.repositoryItemSearchLookUpEdit1View;
            this.DataSource = get设计点(dr["ITEM_VALUE"].ToString(), dr["id"].ToString());

            //  event
            this.Popup += new System.EventHandler(this.searchLookUpEdit设计点_Popup);
            this.repositoryItemSearchLookUpEdit1View.RowCellClick += new DevExpress.XtraGrid.Views.Grid.RowCellClickEventHandler(this.searchLookUpEdit设计点View_RowCellClick);
            this.CustomDisplayText += new DevExpress.XtraEditors.Controls.CustomDisplayTextEventHandler(this.searchLookUpEdit设计点_CustomDisplayText);
        }
        /// <summary>
        /// Excel导入用
        /// </summary>
        /// <param name="Dto设计点"></param>
        public RepositoryItemSearchLookUpEdit设计点(DTODesignPoints Dto设计点)
        {
            this.Dto设计点 = Dto设计点;   //  传入当前设计点
            this.addColumns();  //  下拉view的column

            this.AutoHeight = false;
            this.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
            this.Name = "repositoryItemSearchLookUpEdit" + Dto设计点.itemValue;
            this.PopupView = this.repositoryItemSearchLookUpEdit1View;
            //this.DataSource = get设计点("4SCO", "-186744910542536704");
            this.DataSource = new DataTable();

            //  event
            this.Popup += new System.EventHandler(this.searchLookUpEdit设计点_Popup);
            this.repositoryItemSearchLookUpEdit1View.RowCellClick += new DevExpress.XtraGrid.Views.Grid.RowCellClickEventHandler(this.searchLookUpEdit设计点View_RowCellClick);
            this.CustomDisplayText += new DevExpress.XtraEditors.Controls.CustomDisplayTextEventHandler(this.searchLookUpEdit设计点_CustomDisplayText);
        }

        public static DataTable get设计点(String ITEM_CD,String id) {
            String sql = "";
            String sql1 = "";
            if (ITEM_CD == "4SCON")
            {
                sql = "SELECT\n" +
                        "	a.DESIGN_ID ID,\n" +
                        "	a.STYLE_CATEGORY_CD styleCategoryCD,\n" +
                        "	a.FILE_ID fileID,\n" +
                        "	d.ITEM_CD OPTION_VALUE,\n" +
                        "	a.ITEM_CD ITEM_CD,\n" +
                        "	a.ITEM_VALUE ITEM_VALUE,\n" +
                        "	a.ITEM_NAME_CN ITEM_NAME_CN,\n" +
                        "	file.UPLOAD_FILE \"picn\",\n" +
                        "	CONCAT( 'shirtmtm.com', file.FILE_PATH, file.UPLOAD_FILE ) \"picurl\",\n" +
                        "	a.ITEM_NAME_EN itemNameEN,\n" +
                        "	a.ITEM_NAME_JP itemNameJP,\n" +
                        "	IFNULL( a.ITEM_COST, 0 ) itemCost,\n" +
                        "	a.REMARKS remarks,\n" +
                        "	a.ENABLE_FLAG enableFlag,\n" +
                        "	a.DELETE_FLAG deleteFlag,\n" +
                        "	a.HAVETO_FLAG haveToFlag,\n" +
                        "	a.VERSION version,\n" +
                        "	a.CREATE_DATE createDate,\n" +
                        "	a.CREATE_USER \"createBy.id\",\n" +
                        "	a.UPDATE_USER \"updateBy.id\",\n" +
                        "	a.ITEM_SORT itemSort,\n" +
                        "	a.ITEM_CATEGORY_CD itemCategoryCD,\n" +
                        "	file.FILE_ID \"uploadFile.fileId\",\n" +
                        "	file.FILE_SOURCE \"uploadFile.fileSource\",\n" +
                        "	file.MODULE_KBN \"uploadFile.moduleKbn\",\n" +
                        "	file.FTP_FILE \"uploadFile.ftpFile\",\n" +
                        "	file.FILE_PATH \"uploadFile.filePath\",\n" +
                        "	CONCAT( p.FIRST_NAME, p.LAST_NAME ) \"updateBy.firstName\" \n" +
                        "FROM\n" +
                        "	a_designoption_p a\n" +
                        "	LEFT JOIN a_login_user_p p ON a.UPDATE_USER = p.login_user_id\n" +
                        "	LEFT JOIN w_upload_file_p file ON a.FILE_ID = file.FILE_ID\n" +
                        "	LEFT JOIN a_ognization_desgin_r adr ON a.DESIGN_ID = adr.DESGIN_ID\n" +
                        "	LEFT JOIN a_designoption_p d ON a.ITEM_CD = d.ITEM_VALUE \n" +
                        "WHERE\n" +
                        "	a.STYLE_CATEGORY_CD = 'STYLE_CATEGORY-SHIRT' \n" +
                        "	AND a.DESIGN_ID IN ( SELECT DESGIN_ID FROM a_shop_desgin_r WHERE SHOP_ID = 46 ) \n" +
                        "	AND a.ITEM_CD = '4SCON' \n" +
                        "	AND a.DELETE_FLAG = 0 \n" +
                        "	AND adr.OGNIZATION_ID = 120 -- AND (a.ITEM_VALUE LIKE '%+ str + \"%' OR a.ITEM_NAME_CN LIKE '%+ str + \"%') +\n" +
                        "	\n" +
                        "ORDER BY\n" +
                        "	a.item_sort,\n" +
                        "	a.UPDATE_DATE DESC;";
                sql1 = "SELECT\n" +
                        "	a_kuanshi_p.id AS ID,\n" +
                        "	a_designoption_p.ITEM_CD AS OPTION_VALUE,\n" +
                        "	a_kuanshi_p.ITEM_CD AS `ITEM_CD`,\n" +
                        "	a_kuanshi_p.ITEM_VALUE AS `ITEM_VALUE`,\n" +
                        "	a_kuanshi_p.ITEM_NAME_CN \n" +
                        "FROM\n" +
                        "	a_kuanshi_p\n" +
                        "	LEFT JOIN a_designoption_p ON a_kuanshi_p.ITEM_CD = a_designoption_p.ITEM_VALUE \n" +
                        "WHERE\n" +
                        "	PARENT_ID = '" + id + "';";
            }
            else
            {
                sql = "SELECT\n" +
                    "                a.DESIGN_ID ID,\n" +
                    "                a.STYLE_CATEGORY_CD styleCategoryCD,\n" +
                    "                a.FILE_ID fileID,\n" +
                    "				 d.ITEM_CD ITEM_CD,\n" +
                    "                a.ITEM_CD ITEM_VALUE,\n" +
                    "                a.ITEM_VALUE OPTION_VALUE,\n" +
                    "                a.ITEM_NAME_CN ITEM_NAME_CN,\n" +
                    "                file.UPLOAD_FILE \"picn\",\n" +
                    "                CONCAT( 'shirtmtm.com',file.FILE_PATH, file.UPLOAD_FILE ) \"picurl\",\n" +
                    "                a.ITEM_NAME_EN itemNameEN,\n" +
                    "                a.ITEM_NAME_JP itemNameJP,\n" +
                    "                IFNULL( a.ITEM_COST, 0 ) itemCost,\n" +
                    "                a.REMARKS remarks,\n" +
                    "                a.ENABLE_FLAG enableFlag,\n" +
                    "                a.DELETE_FLAG deleteFlag,\n" +
                    "                a.HAVETO_FLAG haveToFlag,\n" +
                    "                a.VERSION version,\n" +
                    "                a.CREATE_DATE createDate,\n" +
                    "                a.CREATE_USER \"createBy.id\",\n" +
                    "                a.UPDATE_USER \"updateBy.id\",\n" +
                    "                a.ITEM_SORT itemSort,\n" +
                    "                a.ITEM_CATEGORY_CD itemCategoryCD,\n" +
                    "                file.FILE_ID \"uploadFile.fileId\",\n" +
                    "                file.FILE_SOURCE \"uploadFile.fileSource\",\n" +
                    "                file.MODULE_KBN \"uploadFile.moduleKbn\",\n" +
                    "                file.FTP_FILE \"uploadFile.ftpFile\",\n" +
                    "                file.FILE_PATH \"uploadFile.filePath\",\n" +
                    "                CONCAT( p.FIRST_NAME, p.LAST_NAME ) \"updateBy.firstName\"\n" +
                    "                FROM\n" +
                    "                a_designoption_p a\n" +
                    "                LEFT JOIN a_login_user_p p ON a.UPDATE_USER = p.login_user_id\n" +
                    "                LEFT JOIN w_upload_file_p file ON a.FILE_ID = file.FILE_ID \n" +
                    "                LEFT JOIN a_ognization_desgin_r adr ON a.DESIGN_ID = adr.DESGIN_ID\n" +
                    "							  LEFT JOIN a_designoption_p d ON a.ITEM_CD=d.ITEM_VALUE	\n" +
                    "                WHERE\n" +
                    "                a.STYLE_CATEGORY_CD = 'STYLE_CATEGORY-SHIRT' and a.DESIGN_ID IN ( SELECT DESGIN_ID FROM a_shop_desgin_r WHERE SHOP_ID = 46 ) \n" +
                    "                AND a.ITEM_CD='" + ITEM_CD + "'\n" +
                    "                AND a.DELETE_FLAG = 0 \n" +
                    "                AND adr.OGNIZATION_ID = 120 \n" +
                    "                -- AND (a.ITEM_VALUE LIKE '%+ str + \"%' OR a.ITEM_NAME_CN LIKE '%+ str + \"%') +\n" +
                    "                ORDER BY\n" +
                    "                a.item_sort,\n" +
                    "                a.UPDATE_DATE DESC;";
                sql1 = "SELECT a_kuanshi_p.id AS ID,a_designoption_p.ITEM_CD,a_kuanshi_p.ITEM_CD AS `ITEM_VALUE`,a_kuanshi_p.ITEM_VALUE AS `OPTION_VALUE`,a_kuanshi_p.ITEM_NAME_CN FROM a_kuanshi_p LEFT JOIN a_designoption_p ON a_kuanshi_p.ITEM_CD=a_designoption_p.ITEM_VALUE WHERE PARENT_ID='" + id + "';";
            }
            if (SQLmtm.GetDataTable(sql1).Rows.Count == 0)
            {
                return SQLmtm.GetDataTable(sql);
            }
            else
            {
                return SQLmtm.GetDataTable(sql1);
            }
        }

        /// <summary>
        /// 下拉view的column
        /// </summary>
        private void addColumns()
        {
            //id
            DevExpress.XtraGrid.Columns.GridColumn colID = new DevExpress.XtraGrid.Columns.GridColumn();
            this.repositoryItemSearchLookUpEdit1View.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colID });
            colID.Caption = "ID";
            colID.FieldName = "ID";
            colID.Name = "colID";
            colID.Visible = false;
            colID.VisibleIndex = -1;
            //ITEM_CD
            DevExpress.XtraGrid.Columns.GridColumn colITEM_CD = new DevExpress.XtraGrid.Columns.GridColumn();
            this.repositoryItemSearchLookUpEdit1View.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colITEM_CD });
            colITEM_CD.Caption = "ITEM_CD";
            colITEM_CD.FieldName = "ITEM_CD";
            colITEM_CD.Name = "colITEM_CD";
            colITEM_CD.Visible = false;
            colITEM_CD.VisibleIndex = -1;
            //ITEM_VALUE
            DevExpress.XtraGrid.Columns.GridColumn colITEM_VALUE = new DevExpress.XtraGrid.Columns.GridColumn();
            this.repositoryItemSearchLookUpEdit1View.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colITEM_VALUE });
            colITEM_VALUE.Caption = "ITEM_VALUE";
            colITEM_VALUE.FieldName = "ITEM_VALUE";
            colITEM_VALUE.Name = "colITEM_VALUE";
            colITEM_VALUE.Visible = false;
            colITEM_VALUE.VisibleIndex = -1;
            //OPTION_VALUE
            DevExpress.XtraGrid.Columns.GridColumn colOPTION_VALUE = new DevExpress.XtraGrid.Columns.GridColumn();
            this.repositoryItemSearchLookUpEdit1View.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colOPTION_VALUE });
            //colOPTION_VALUE.Caption = "OPTION_VALUE";
            colOPTION_VALUE.Caption = "编码";
            colOPTION_VALUE.FieldName = "OPTION_VALUE";
            colOPTION_VALUE.Name = "colOPTION_VALUE";
            colOPTION_VALUE.Visible = true;
            colOPTION_VALUE.VisibleIndex = 0;
            //ITEM_NAME_CN
            DevExpress.XtraGrid.Columns.GridColumn colITEM_NAME_CN = new DevExpress.XtraGrid.Columns.GridColumn();
            this.repositoryItemSearchLookUpEdit1View.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colITEM_NAME_CN });
            colITEM_NAME_CN.Caption = "名称";
            colITEM_NAME_CN.FieldName = "ITEM_NAME_CN";
            colITEM_NAME_CN.Name = "colITEM_NAME_CN";
            colITEM_NAME_CN.Visible = true;
            colITEM_NAME_CN.VisibleIndex = 1;
        }

        #region 选择设计点

        private void searchLookUpEdit设计点View_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            //this.ITEM_CD = FunctionHelper.GetgridViewValue((GridView)sender, e.RowHandle, "ITEM_CD");
            //this.ITEM_VALUE = FunctionHelper.GetgridViewValue((GridView)sender, e.RowHandle, "ITEM_VALUE");
            //this.OPTION_VALUE = FunctionHelper.GetgridViewValue((GridView)sender, e.RowHandle, "OPTION_VALUE");
            //this.ITEM_NAME_CN = FunctionHelper.GetgridViewValue((GridView)sender, e.RowHandle, "ITEM_NAME_CN");
            ////this.ITEM_VALUE= FunctionHelper.GetgridViewValue((GridView)sender, e.RowHandle, "ITEM_VALUE");
            //this.Dto设计点.ITEM_CD = this.ITEM_CD;
            //this.Dto设计点.ITEM_VALUE = this.ITEM_VALUE;
            //this.Dto设计点.OPTION_VALUE = this.OPTION_VALUE;
            //this.Dto设计点.Name = this.ITEM_NAME_CN;
            ////this.DisplayMember = this.ITEM_NAME_CN;
            this.Dto设计点.itemCd = FunctionHelper.GetgridViewValue((GridView)sender, e.RowHandle, "ITEM_CD");
            this.Dto设计点.itemValue = FunctionHelper.GetgridViewValue((GridView)sender, e.RowHandle, "ITEM_VALUE");
            this.Dto设计点.optionValue = FunctionHelper.GetgridViewValue((GridView)sender, e.RowHandle, "OPTION_VALUE");
            this.Dto设计点.name = FunctionHelper.GetgridViewValue((GridView)sender, e.RowHandle, "ITEM_NAME_CN");
        }

        /// <summary>
        /// 绑定清空事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit设计点_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                ////得到取消按钮，重写点击事件
                //Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                //LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                //clearBtn.Click += clearBtn_Click;

                ////添加一个simplebutton控件(确认按钮)
                //LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                //myLCI.TextVisible = false;
            }
        }
        

        /// <summary>
        /// 选完之后的显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit设计点_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            //((SearchLookUpEdit)sender).Properties.View.GetRowCellValue(rowHandle, "ITEM_VALUE").ToString()
            if (null != e.Value)
                //e.DisplayText = this.OPTION_VALUE + ":" + this.ITEM_NAME_CN;
            e.DisplayText = this.Dto设计点.optionValue + ":" + this.Dto设计点.name;
        }

        private void searchLookUpEdit设计点_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name == "colOPTION_VALUE")
            {
                if (this.Dto设计点.optionValue != this.Dto设计点.defaultValue)
                {
                    e.Appearance.BackColor = Color.Red;
                }
            }
        }
        #endregion
    }
}
